package com.four.order.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.four.order.dao.OrderDao;
import com.four.order.pojo.Order;
import com.four.order.util.DBUtil;

public class OrderDaoImpl implements OrderDao{

	@Override
	public List<Order> selectAll() {
		List<Order> list = new ArrayList<>();
		Connection conn = DBUtil.getConnection();
		String sql = "select * from t_order where oflag = 1";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				Order o = new Order();
				o.setId(rs.getString("id"));
				o.setName(rs.getString("name"));
				o.setOaddress(rs.getString("oaddress"));
				o.setCreatetime(rs.getString("createtime"));
				o.setOoldprice(rs.getString("ooldprice"));
				o.setOnowprice(rs.getString("onowprice"));
				o.setCount(rs.getString("count"));
				o.setEndtime(rs.getString("endtime"));
				o.setOflag(rs.getString("oflag"));
				o.setOremark(rs.getString("oremark"));
				list.add(o);
				o = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.closeResource(conn, ps, rs);
		}
		return list;
	}



	public Order selectByName(String name) {
		Connection conn = DBUtil.getConnection();
		String sql = "select * from order where name = ?";
		PreparedStatement ps = null;
		ResultSet rs= null;
		System.out.println("Dao - id: " + name);
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			if(rs.next()) {
				Order o = new Order();
				o.setId(rs.getString("id"));
				o.setName(rs.getString("name"));
				o.setOaddress(rs.getString("oaddress"));
				o.setCreatetime(rs.getString("createtime"));
				o.setOoldprice(rs.getString("ooldprice"));
				o.setOnowprice(rs.getString("onowprice"));
				o.setCount(rs.getString("count"));
				o.setEndtime(rs.getString("endtime"));
				o.setOflag(rs.getString("oflag"));
				o.setOremark(rs.getString("oremark"));
				return o;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public int addOrder(long id,int name,String oaddress,String createtime,String ooldprice,String onowprice,String count,String endtime,String oflag,String oremark) {
		Connection conn = DBUtil.getConnection();
		String sql = "INSERT INTO `order` (`id`, `name`, `oaddress`, `createtime`, `ooldprice`, `onowprice`,`count`,`endtime`,`oflag`,`oremark`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		PreparedStatement ps = null;
		int n = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			ps.setInt(2, name);
			ps.setString(3, oaddress);
			ps.setString(4, createtime);
			ps.setString(5, ooldprice);
			ps.setString(6, onowprice);
			ps.setString(7, count);
			ps.setString(8, endtime);
			ps.setString(9, oflag);
			ps.setString(10, oremark);
			ps.setInt(11, 1);
			n = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.closeResource(conn, ps, null);
		}
		return n;
		
	}
	
	@Override
	public int updateOrder(long id,int name,String oaddress,String createtime,String ooldprice,String onowprice,String count,String endtime,String oflag,String oremark) {
		Connection conn = DBUtil.getConnection();
		String sql = "UPDATE `order` SET `name`=?, `oaddress`=?,"
				+ " `createtime`=?, `ooldprice`=?, `onowprice`=?, `count`=?, `endtime`=?, `oflag`=?, `oremark`=? WHERE (`id`=?)";
		PreparedStatement ps = null;
		int n = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			ps.setInt(2, name);
			ps.setString(3, oaddress);
			ps.setString(4, createtime);
			ps.setString(5, ooldprice);
			ps.setString(6, onowprice);
			ps.setString(7, count);
			ps.setString(8, endtime);
			ps.setString(9, oflag);
			ps.setString(10, oremark);
			n = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.closeResource(conn, ps, null);
		}
		return n;
	
	}
	
	@Override
	public int deleteOrder(long id) {
		Connection conn = DBUtil.getConnection();
		String sql = "update order set flag=0 where id=?";
		PreparedStatement ps = null;
		int n = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			n = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.closeResource(conn, ps, null);
		}
		return n;
	}



	@Override
	public Order selectById(long id) {
		Connection conn = DBUtil.getConnection();
		String sql = "select * from order where id = ?";
		PreparedStatement ps = null;
		ResultSet rs= null;
		System.out.println("Dao - name: " + id);
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			rs = ps.executeQuery();
			if(rs.next()) {
				Order o = new Order();
				o.setId(rs.getString("id"));
				o.setName(rs.getString("name"));
				o.setOaddress(rs.getString("oaddress"));
				o.setCreatetime(rs.getString("createtime"));
				o.setOoldprice(rs.getString("ooldprice"));
				o.setOnowprice(rs.getString("onowprice"));
				o.setCount(rs.getString("count"));
				o.setEndtime(rs.getString("endtime"));
				o.setOflag(rs.getString("oflag"));
				o.setOremark(rs.getString("oremark"));		
				return o;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	
	}


}
